package br.unifor.huffmain.main;

import java.io.IOException;
import java.io.File;
import java.util.BitSet;

/**
 * This project implements the compression and decompression of UTF-8-encoded files
 * through adaptive Huffman coding using the FGK-algorithm defined by Faller,
 * Gallager and Knuth to maintain the coding tree.
 */
public class Principal {

	// TODO - Escrever menu
	
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        if (args.length < 3) {
            printUsage();
        } else {
            long start = System.currentTimeMillis();
            
            if (args[0].equals("comprimir")) {
                for (int i = 0; i < 10; i++) {
                    Encodificador encoder = new Encodificador();
                    encoder.compressFile(args[1], args[2]);
                }
            } else if (args[0].equals("descomprimir")) {
                for (int i = 0; i < 10; i++) {
                    Decodificador decoder = new Decodificador();
                    decoder.decompressFile(args[1], args[2]);
                }
            } else {
                System.err.println("opcao desconhecida: " + args[0]);
                printUsage();
            }
            
            long end = System.currentTimeMillis();
            File in = new File(args[1]);
            File out = new File(args[2]);
            long inBytes = in.length();
            long outBytes = out.length();
            
            System.out.println("Tempo total para compressao " + (end - start) + "millisegundos.");
            System.out.println("Taxa de compressao: " + ((double) outBytes / (double) inBytes));
        }
    }
    
    private static void printUsage() {
        System.out.println("Adaptive Huffman compressor usage:");
        System.out.println("To compress:");
        System.out.println("./java -jar adaptiveHuffmanCoding.jar compress input.txt compressed.bin");
        System.out.println("To decompress:");
        System.out.println("./java -jar adaptiveHuffmanCoding.jar decompress compressed.bin output.txt");
    }
}
